package com.gofunbuy.app.store;

import java.util.List;

import com.gofunbuy.R;
import com.gofunbuy.core.ProductEntity;
import com.gofunbuy.util.AsyncImageLoader;
import com.gofunbuy.util.AsyncImageLoader2.ImageCallback;
import com.gofunbuy.util.GoFunBuyUtils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AppProductAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<ProductEntity> mData;
	private ListView mListView;

	//private AsyncImageLoader asyncImageLoader;

	public AppProductAdapter(Context context, ListView listView) {
		this.mInflater = LayoutInflater.from(context);
		this.mListView = listView;
		//asyncImageLoader = new AsyncImageLoader();
	}

	public void setDatas(List<ProductEntity> data) {
		mData = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mData == null) {
			return 0;
		}
		return mData.size();
	}

	@Override
	public ProductEntity getItem(int arg0) {
		// TODO Auto-generated method stub
		return mData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.product_item_layout, null);
			holder.img = (ImageView) convertView
					.findViewById(R.id.product_item_img);
			holder.name = (TextView) convertView
					.findViewById(R.id.product_item_name);
			holder.price = (TextView) convertView
					.findViewById(R.id.product_item_price);
			holder.book = (TextView) convertView
					.findViewById(R.id.product_item_book);

			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();
		}
		ProductEntity pe = mData.get(position);

		holder.name.setText(pe.getName());
		holder.price.setText("HK$" + pe.getPrice());

		if (pe.getImg() != null) {
			holder.img.setImageDrawable(pe.getImg());

		} else if (pe.getImgUri() != null && !"".equals(pe.getImgUri())) {
			String imageUrl = pe.getImgUri();
			holder.img.setTag(imageUrl);

			Drawable cachedImage = GoFunBuyUtils.asyncImageLoader.loadBigDrawable(imageUrl,
				new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						ImageView imageViewByTag = (ImageView) mListView
								.findViewWithTag(imageUrl);
						if (imageViewByTag != null) {
							imageViewByTag.setImageDrawable(imageDrawable);
						}

					}
				});
			if (cachedImage == null) {
				holder.img.setImageResource(R.drawable.loading);
			} else {
				holder.img.setImageDrawable(cachedImage);
			}
		}

		holder.book.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		return convertView;
	}

	public final class ViewHolder {
		public ImageView img;
		public TextView name;
		public TextView price;
		public TextView book;
	}
}